import operator
from abc import ABC, abstractmethod

from facade.mysqlfacade import MysqlUtiles
from facade.sqlite3facade import Sqlite3Utiles


class TableContrast(ABC):

    def __init__(self):
        self.sqlconn1 = None
        self.sqlconn2 = None

    def mysqlconn(self, configfile, sesc):
        # 配置文件与默认配置文件key的转换
        keytransformdicts = {"port": "db_port", "host": "db_host", "passwd": "db_pw", "user": "db_user",
                             "db": "db_name",
                             "chartset": "db_charset"}
        # mysql 工具类
        myutil = MysqlUtiles(configfile, sesc, keytransformdicts=keytransformdicts)
        return myutil

    def sqlite3conn(self,filePath):
        # 获取sqlite处理对象，通过传入的db3文件
        sqliteutiles = Sqlite3Utiles().Sqlite3DBConnectFromFilePath(filePath)
        return sqliteutiles

    @abstractmethod
    def get_one_table_data(self):
        pass

    @abstractmethod
    def one_table_data_hook(self):
        """
        该函数需要返回两个对象
        一个是该表的key列表
        一个是该表以key为键 数据为值的字典
        :return:
        """
        pass

    @abstractmethod
    def get_other_table_data(self):
        pass

    @abstractmethod
    def other_table_data_hook(self):
        """
        该函数需要返回两个对象
        一个是该表的key列表
        一个是该表以key为键 数据为值的字典
        :return:
        """
        pass

    def contrast_tow_table(self, key1, dicts1, key2, dicts2):
        # 对比出来不同的key
        differentkey = []
        # key的合集
        keyand = list(set(key1) & set(key2))
        # 差集1
        keySU1 = list(set(key1) ^ set(keyand))
        # 差集2
        keySU2 = list(set(key2) ^ set(keyand))

        for key in keyand:
            # 得到对应的值 值是键值对
            rows1 = dicts1[key]
            rows2 = dicts2[key]
            # 比对两个数据
            bool_result = operator.eq(rows1, rows2)
            if not bool_result:
                # 将不一样的数据提取出来
                differentkey.append(key)

        keysame = list(set(keyand) ^ set(differentkey))
        # 分别代表第一张表独有的，第二张独有的，比对后不相同的，比对后相同的
        return keySU1, keySU2, differentkey, keysame


